<?php

function calculateBgWidth($a, $b) {
	$width = -435;
	if ($a+$b) {
		$width = round($b/($a+$b) * 870 * (-1));
	}
	return $width;
}

function aktivnoDugme(array $niz) {
	$page = "home";
	if (isset($_GET['page'])) $page = $_GET['page'];
	
	if (in_array($page, $niz)) {
		echo "active ";
	}
}

function loadCss($strana, array $niz) {
	$page = "home";
	if (isset($_GET['page'])) {
		$page = $_GET['page'];
	}
	if ($page == $strana) {
		foreach ($niz as $stil) {
			echo "<link href='template/css/" . $stil . ".css' type='text/css' rel='stylesheet' media='all' />\n";
		}
	}
}

function isLoggedIn() {
	if (isset($_SESSION['bl_user_id'])) return 1;
	else return 0;
}

function percentage($val1, $val2, $precision)  {
	$res = round( ($val1 / $val2) * 100, $precision );
	return $res;
}		



	function getAllLeagues() {
		$niz = array();
		$q = "SELECT id FROM liga";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Liga($n['id']);
		}
		return $niz;
	}
	
	function getAllMatches() {
		$niz = array();
		$q = "SELECT id FROM utakmica";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Match($n['id']);
		}
		return $niz;
	}
	
	function getAllTeams() {
		$niz = array();
		$q = "SELECT id FROM tim";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Tim($n['id']);
		}
		return $niz;
	}

	function getAllPlayers() {
		$niz = array();
		$q = "SELECT id FROM igrac";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Player($n['id']);
		}
		return $niz;
	}

	function getAllRounds() {
		$niz = array();
		$q = "SELECT id FROM kolo";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Round($n['id']);
		}
		return $niz;
	}

	function getAllPlayersByPosition($pos) {
		$niz = array();
		$q = "SELECT `igrac` FROM `pozicije` WHERE `pozicija` = '".$pos."'";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Player($n['igrac']);
		}
		return $niz;
	}
	
	function sortPlayers($array, $parameter) {
		$k = true;
		$n = count($array);
		while ($k) {
			$k = false;
			for ($i=0; $i<$n-1; $i++) {
				if ($array[$i]->totalStats->$parameter > $array[$i+1]->totalStats->$parameter) {
					list($array[$i], $array[$i+1]) = array($array[$i+1], $array[$i]);
					$k = true;
				}
			}
		}
	}

	function getAllArticles() {
		$niz = array();
		$q = "SELECT id FROM vest";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Article($n['id']);
		}
		return $niz;
	}
	
	function getMatchByTeams($a, $b) {
		$q = "SELECT id FROM utakmice WHERE (domacin = '".$a->id."' and gost = '".$b->id."') OR (domacin = '".$b->id."' AND gost = '".$a->id."')";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		$mec = NULL;
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$mec = new Match($n['id']);
		}
		return $mec;
	}
	
	/* pozivanje preko url-a */
	
	if (isset($_GET['call'])) {
		
		function __autoload($class_name) {
			include '../class/' . $class_name . '.php';
		}
		include("connection.php");
		
		if (function_exists($_GET['call'])) {
				call_user_func($_GET['call']);
		}
	}


?>